﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace goMatic
{
    public partial class SWITCH : System.Web.UI.UserControl
    {
        public event EventHandler StateChanged;

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public SWITCH()
        {
        }

        v_HMDeviceDetails vDetail;
        public v_HMDeviceDetails _Details
        {
            get { return vDetail; ; }
            set
            {
                if (value != null)
                {
                    vDetail = value;
                    dName.Text = vDetail.Name;
                    lDevice.Text = vDetail.Address;
                    if (vDetail.value != null)
                    {
                        _State = Convert.ToBoolean(int.Parse(vDetail.value));
                        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
                        TimeSpan offset = tzi.GetUtcOffset(DateTime.Now);
                        lTime.Text = (value.C__createdAt.Value.LocalDateTime + offset).ToString("dd-MM HH:mm:ss");
                    }
                    else
                    {
                        ImageButton1.ImageUrl = "~/Images/unknown.png";
                    }
                }
                else
                {
                    ImageButton1.ImageUrl = "~/Images/unknown.png";
                }
            }
        }

        internal Boolean bState;
        public Boolean _State
        {
            get
            {
                return bState;
            }
            set
            {
                bState = value;
                if (bState)
                {
                    ImageButton1.ImageUrl = "~/Images/control_on_32.png";
                }
                else
                {
                    ImageButton1.ImageUrl = "~/Images/control_off_32.png";
                }
            }
        }

        public bool ReadOnly { get; set; }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (vDetail.IsWritable & !ReadOnly)
                {
                    if (_State != null)
                    {
                        _State = !bState;
                        vDetail.value = (_State ? 1 : 0).ToString();
                        if (this.StateChanged != null)
                        {
                            this.StateChanged(this, e);
                        }
                        CCU.ccucmdSoapClient oCMD = new CCU.ccucmdSoapClient();
                        oCMD.SetValueAsync("", vDetail.Interface, vDetail.Address, "STATE", "boolean", (_State ? 1 : 0).ToString(), vDetail.CustomerID);
                    }
                    else
                    {
                        //Switch has no value, get it now...
                        CCU.ccucmdSoapClient oCMD = new CCU.ccucmdSoapClient();
                        //oCMD.getValue;
                    }
                }
            }
            catch(Exception ex)
            {
                ex.Message.ToString();
                ImageButton1.ImageUrl = "~/Images/unknown.png";
            }
        }
    }
}